import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { productApi } from "@/api/erp/drug/drug";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { useState, useMemo } from "react";
let reload: Function;
let productId: any;
const index = (props: { register: Fn }) => {
	const [isUpdated, setIsUpdated] = useState(false);
	const { t } = useTranslation();
	const en_vi = "drugStorage.supplies.suppliesInfo";
	const formSchema: FormSchema[] = [
		{
			label: t(`${en_vi}.cseName`),
			field: "cseName",
			component: "Input"
		},
		{
			label: t(`${en_vi}.cseUnit`),
			field: "cseUnit",
			component: "Input"
		},
		{
			label: t(`${en_vi}.cseType`),
			component: "Select",
			field: "cseType",
			componentProps: {
				options: [
					// 暂时没定
					{ label: t(`手术类(测试,没国际化)`), value: "手术类(测试,没国际化)" },
					{ label: t(`检验类(测试,没国际化)`), value: "检验类(测试,没国际化)" }
				]
			}
		},
		// {
		// 	label: t(`${en_vi}.rhFactor`),
		// 	field: "rhFactor",
		// 	component: "RadioGroup",
		// 	componentProps: {
		// 		options: [
		// 			{ label: t(`RH+`), value: "+" },
		// 			{ label: t(`RH-`), value: "-" }
		// 		]
		// 	}
		// },
		{
			label: t(`${en_vi}.supperId`),
			field: "supperId",
			component: "Input"
		},
		{
			label: t(`${en_vi}.model`),
			field: "model",
			component: "Input"
		},
		{
			label: t(`${en_vi}.storageCond`),
			field: "storageCond",
			component: "Input"
		},
		{
			label: t(`${en_vi}.expiredDate`),
			field: "expiredDate",
			component: "DatePicker"
		},
		{
			label: t("felix_ui.status"),
			field: "status",
			component: "RadioGroup",
			componentProps: {
				options: [
					{ label: t(`felix_ui.open`), value: "0" },
					{ label: t(`felix_ui.close`), value: "1" }
				]
			}
		}
	];
	const getTitle = useMemo(() => {
		return isUpdated ? t(`felix_ui.edit`) : t(`felix_ui.add`);
	}, [isUpdated]);
	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 120,
		baseColProps: {
			span: 24
		}
	});
	const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(props.register, data => {
		reload = data.reload;
		productId = data.record?.productId;
		resetFields();
		setDrawerProps({ confirmLoading: true });
		setIsUpdated(!!data?.isUpdated);
		setFieldsValue({
			...data.record
		});
		setDrawerProps({ confirmLoading: false });
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		formValue.expiredDate = new Date(formValue.expiredDate).getTime();
		formValue.consumables = { ...formValue, cseId: productId };
		formValue.bizType = "4";
		formValue.productName = formValue.cseName;
		let res;
		if (isUpdated) {
			formValue.productId = productId;
			res = await productApi.updateproduct(formValue);
		} else {
			res = await productApi.createproduct(formValue);
		}
		if (!res) {
			message.error(t("viewComponents.patientInformation.additionFailed"));
			return;
		}
		message.success(t("viewComponents.patientInformation.successfullyAdded"));
		closeDrawer();
		reload();
	};
	return (
		<BasicDrawer title={getTitle} showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={register} labelWrap={true}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
